package libs.yzx.zaza.acts2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import libs.yzx.zaza.R;



/**
 * Created by Administrator on 2016/9/30
 */

public class AppBarLayoutActivity extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_app_bar_layout);

        RecyclerView rv = (RecyclerView) findViewById(R.id.recyclerView);
        rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        rv.setHasFixedSize(true);
        rv.setAdapter(new Adapter());



        AppBarLayout abl = (AppBarLayout) findViewById(R.id.appBarLayout);
        final View topView = findViewById(R.id.hidden);
        abl.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                /* 根据有没有tag判断是否执行过了 */
                if(topView.getTag()!=null) return ;
                /* 向上滑动的距离<= 那个view的高度(是负数,因为向上滑动) */
                if(verticalOffset <= -topView.getMeasuredHeight()) {
                    ViewGroup parent = (ViewGroup) topView.getParent();
                    parent.removeView(topView);
                    /* 标记一下,表示已经移除过了 */
                    topView.setTag("");
                }
            }
        });
    }


    private class Adapter extends RecyclerView.Adapter<Holder>{
        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent,false);
            return new Holder(view);
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            TextView tv = (TextView) holder.view.findViewById(R.id.tv);
            tv.setText(""+position);
        }

        @Override
        public int getItemCount() {
            return 30;
        }
    }

    private class Holder extends RecyclerView.ViewHolder{
        public final View view;
        public Holder(View itemView) {
            super(itemView);
            view = itemView;
        }
    }


}
